﻿Imports System.ComponentModel
Imports Global_Visas_Business_Layer
Imports System.Text
Imports System.Data

Partial Public Class frmDocumentStore


    Private Sub frmDocumentStore_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()

        End If
    End Sub

    Private Sub frmDocumentStore_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        With Me.drpDocType
            .SelectedValuePath = "docreqtype_id"
            .DisplayMemberPath = "DocReqType_Name"
            .ItemsSource = CType(Documents.GetDocReqTypes, IListSource).GetList
            .SelectedIndex = -1
        End With


        DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)
        DataBinding.BindListView(Documents.GetCaseDocsInStore(GlobalCRM.CurrentCaseID), Me.lvwStored)
        'btnDefaults_Click(sender, e)
        With cmbDocComments
            .DisplayMemberPath = "DocComment_Msg"
            .SelectedValuePath = "DocComment_Id"
            .ItemsSource = CType(Documents.DocCommentList, IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub



    Private Sub btnReceived_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnReceived.Click
        Dim Docs As New StringBuilder
        Dim Msg As New StringBuilder

        If Me.lvwRequired.SelectedItems.Count = 0 Then
            MsgBox("No documents selected!", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else

            Dim lvi As DataRowView

            For Each lvi In Me.lvwRequired.SelectedItems

                If lvi.Row.ItemArray(4) <> "" Then
                    MsgBox("Some of those documents have already been received, you cannot receive them again.", MsgBoxStyle.Exclamation, "Error")
                    Exit Sub
                End If


                If Docs.ToString = "" Then
                    Docs.Append("(")
                Else
                    Docs.Append(", ")
                End If

                Docs.Append(lvi.Row.ItemArray(0))
                Msg.Append(lvi.Row.ItemArray(2).ToString & " : " & lvi.Row.ItemArray(1).ToString & vbCrLf)


            Next

            Docs.Append(")")

        End If

        Msg.Insert(0, "The following docs have been marked as received" & vbCrLf & vbCrLf)

        If Not Documents.UpdateReceivedDate(GlobalCRM.CurrentConName, Docs.ToString, Msg.ToString, GlobalCRM.CurrentCaseID) Then
            Exit Sub
        End If

        'Call GblFunc_Populate_Case_Labels()

        DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)

    End Sub

    Private Sub btnDefaults_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDefaults.Click
        Dim ProductId As Long = Cases.GetCaseInfo(GlobalCRM.CurrentCaseID).Productid
        Dim Msg As New StringBuilder
        Dim FirstCase As Integer
        Dim SQL As New StringBuilder

        If ProductId = 0 Then Exit Sub
        If Documents.CheckDefaultDocument(ProductId) = 0 Then
            'If DB.GetSQLScalar("Select DefDocReq_ID from D_DefDocReqs where DefDocReq_ProductID = " & ProductId) = 0 Then
            MsgBox("No default document requirements found for product " & Products.GetProductName(ProductId) & ". Please speak to an adminstrator", , "Document")
            Exit Sub
        End If

        If Documents.GetDefaultDocument(GlobalCRM.CurrentCaseID) > 0 Then

            'If DB.GetSQLScalar(SQL.ToString) > 0 Then

            Msg.Append("Do you want to remove the existing requirements?" & vbCrLf)
            Msg.Append("(Received requirements will not be affected.)")

            If MsgBox(Msg.ToString, vbYesNo, "Add Defaults") = MsgBoxResult.Yes Then

                If Not Documents.DeleteDocRequirements(GlobalCRM.CurrentCaseID) Then
                    Exit Sub
                End If
            End If

        End If

        Msg.Insert(0, "Default doc reqs loaded onto case" & vbCrLf)

        'Check for previous initiated cases on this account

        If Documents.GetDefaultDocumentCount(GlobalCRM.CurrentCaseID, GlobalCRM.CurrentCandidateID, GlobalCRM.CurrentCompanyID) = 0 Then

            'If DB.GetSQLScalar(SQL.ToString) = 0 Then
            FirstCase = 1
        Else
            FirstCase = 0
        End If

        'Add Document requirements
        'If Documents.CreateDocumentRequirements(GlobalCRM.CurrentCaseID, ProductId, GlobalCRM.CurrentCompanyID, Msg.ToString, GlobalCRM.CurrentConName, FirstCase) Then
        '    DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)
        'End If

        If Documents.CreateDocumentRequirementsLoad(GlobalCRM.CurrentCaseID, ProductId, GlobalCRM.CurrentCompanyID, Msg.ToString, GlobalCRM.CurrentConName, FirstCase) Then
            DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)
        End If

        'Call GblFunc_Populate_Case_Labels()

    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnclose.Click
        Me.Close()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
        Dim Docs As New StringBuilder
        Dim Msg As New StringBuilder
        Dim Received As Boolean

        If Me.lvwRequired.SelectedItems.Count = 0 Then
            MsgBox("No documents selected!", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else

            Dim lvi As DataRowView

            For Each lvi In Me.lvwRequired.SelectedItems

                If lvi.Row.ItemArray(4).ToString.Trim <> "" Then
                    Received = True
                End If

                If Docs.ToString = "" Then
                    Docs.Append("(")
                Else
                    Docs.Append(", ")
                End If

                Docs.Append(lvi.Row.ItemArray(0).ToString)
                Msg.Append(lvi.Row.ItemArray(2).ToString & " : " & lvi.Row.ItemArray(1).ToString & vbCrLf)

            Next

            Docs.Append(")")

        End If

        If Received Then
            If MsgBox("Some of those documents have been marked as received, are you sure you want to delete them?", vbYesNo + vbQuestion, "Confirm") <> MsgBoxResult.Yes Then Exit Sub
        End If

        If MsgBox("The following document requirements have been selected for deletion, are you sure you wish to continue?" & vbCrLf & vbCrLf & Msg.ToString, vbYesNo + vbQuestion, "Confirm delete") <> MsgBoxResult.Yes Then Exit Sub


        If Documents.DeleteDocRequirementList(GlobalCRM.CurrentCaseID, Docs.ToString, Msg.ToString) Then
            DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)
        End If

        'Call GblFunc_Populate_Case_Labels()

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
        If Me.txtDocName.Text.Trim = "" Then
            MsgBox("Please provide a valid document name", MsgBoxStyle.Exclamation, "Error")
            Me.txtDocName.Focus()
            Exit Sub
        End If

        If Me.txtDocName.Text.Trim.Length > 200 Then
            MsgBox("Doc Name too long (Max 200 chars)", MsgBoxStyle.Exclamation, "Error")
            Me.txtDocName.Focus()
            Exit Sub
        End If

        If Me.drpDocType.SelectedIndex = -1 Then
            MsgBox("Please select a document type", MsgBoxStyle.Exclamation, "Error")
            Me.drpDocType.Focus()
            Exit Sub
        End If

        If Documents.AddNewDocReq(GlobalCRM.CurrentCaseID, Me.txtDocName.Text.Trim, Me.drpDocType.SelectedValue, Me.drpDocType.Text, GlobalCRM.CurrentConName) > 0 Then
            DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)
        End If

        'Call GblFunc_Populate_Case_Labels()

        Me.txtDocName.Text = ""
        Me.drpDocType.SelectedIndex = -1
    End Sub
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim frmLoadToDocStore As New frmLoadToDocStore
        frmLoadToDocStore.ShowDialog()
        DataBinding.BindListView(Documents.GetCaseDocsInStore(GlobalCRM.CurrentCaseID), Me.lvwStored)
    End Sub

    Private Sub lvwStored_MouseDoubleClick1(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lvwStored.MouseDoubleClick
        If Me.lvwStored.SelectedItems.Count = 0 Then
            MsgBox("Please select a Doc Store.", , "Doc Store")
            Exit Sub
        End If
        Dim frmDocumentList As New frmDocumentList
        frmDocumentList.DocStoreID = lvwStored.SelectedItem.row.itemarray(0)

        frmDocumentList.ShowDialog()

    End Sub

    Private Sub btnAddComment_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddComment.Click
        Dim Docs As New StringBuilder
        Dim Msg As New StringBuilder

        If Me.lvwRequired.SelectedItems.Count = 0 Then
            MsgBox("No documents selected!", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        ElseIf cmbDocComments.SelectedIndex = -1 Then
            MsgBox("No comment Selected!", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else

            Dim lvi As DataRowView

            For Each lvi In Me.lvwRequired.SelectedItems

                'If lvi.SubItems(4).Text.Trim <> "" Then
                '    MsgBox("Some of those documents have already been received, you cannot receive them again.", MsgBoxStyle.Exclamation, "Error")
                '    Exit Sub
                'End If
                If lvi.Row.ItemArray(4).Tostring.Trim = "" Then
                    MsgBox("Some of the selected documents have not been received yet, you cannot comment on them. Please unselect those items and add comment", MsgBoxStyle.Exclamation, "Error")
                    Exit Sub
                End If

                If Docs.ToString = "" Then
                    Docs.Append("(")
                Else
                    Docs.Append(", ")
                End If

                Docs.Append(lvi.Row.ItemArray(0).Tostring)
                Msg.Append(lvi.Row.ItemArray(2).Tostring & " : " & lvi.Row.ItemArray(1).Tostring & vbCrLf)


            Next

            Docs.Append(")")

        End If

        Msg.Insert(0, "The comment for the following document(s) has been uploaded" & vbCrLf & vbCrLf)
        If Not Documents.UpdateDocComments(Docs.ToString, cmbDocComments.SelectedValue, cmbDocComments.Text, GlobalCRM.CurrentCaseID) Then
            Exit Sub
        End If

        'Call GblFunc_Populate_Case_Labels()

        DataBinding.BindListView(Documents.GetDocsRequiredForCase(GlobalCRM.CurrentCaseID), Me.lvwRequired)

    End Sub

End Class
